<template>
  <div>
    <!-- 头部区域 -->
    <div class="header">
      <div class="left">
        <!-- 头像区域 -->
        <div class="avatar">
          <img :src="InfoList.userFace" alt="" srcset="">
        </div>
      </div>
      <div class="right">
        <span @click="Upimg">修改头像</span>
      </div>
    </div>
    <!-- 个人信息区域 -->
    <div class="userInfo">
      <el-form label-position="left" label-width="80px">
        <el-form-item label="登录用户">
          <div>{{ InfoList.username }}</div>
        </el-form-item>
        <el-form-item label="用户姓名">
          <div>{{ InfoList.realname }}</div>
        </el-form-item>
        <el-form-item label="用户手机">
          <div>{{ InfoList.telephone }}</div>
        </el-form-item>
        <el-form-item label="用户性别">
          <div v-if="InfoList.gender == 'male'">
            <span>男</span> <i class="el-icon-male" type="danger"></i>
          </div>
          <div v-else>
            <span>女</span> <i class="el-icon-female" type="danger"></i>
          </div>
        </el-form-item>
        <el-form-item label="用户角色" v-for="item in InfoList.roles" :key="item.id">
          <div>{{ item.name}}</div>
        </el-form-item>
        <el-form-item label="注册时间">
          <div>{{ InfoList.registerTime | fmtDate_global}}</div>
        </el-form-item>
        <el-form-item label="邮箱">
          <div>{{ InfoList.email }}</div>
        </el-form-item>
        <el-form-item label="用户状态">
          <div>
            <el-button type="primary" disabled>{{ InfoList.status }}</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <div class="footer">
      <el-button type="primary" @click="upInfo">修改用户信息</el-button>
    </div>

    <!-- 修改头像模态框 -->
    <el-dialog title="修改头像" :visible.sync="ImgdialogVisible" width="30%">
      <el-upload class="avatar-uploader" :action="uploadFileURL" :show-file-list="false"
        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="upImageForm.userface" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ImgdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upImage">确定修改</el-button>
      </span>
    </el-dialog>


    <!-- 修改用户信息的模态框 -->
    <el-dialog title="修改个人信息" :visible.sync="dialogInfoFormVisible">
      <el-form :model="InfoList" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <!-- 登录用户 -->
        <el-form-item label="登录用户" prop="username">
          <el-input v-model="InfoList.username" disabled></el-input>
        </el-form-item>

        <!-- 姓名 -->
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="InfoList.realname" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="InfoList.gender" label="male">男</el-radio>
          <el-radio v-model="InfoList.gender" label="female">女</el-radio>
        </el-form-item>
        <!-- 用户手机 -->
        <el-form-item label="用户手机" prop="telephone">
          <el-input v-model="InfoList.telephone"></el-input>
        </el-form-item>
        <!-- 用户邮箱 -->
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="InfoList.email"></el-input>
        </el-form-item>
        <!-- 出生日期 -->
        <el-form-item label="出生日期" required>
          <el-form-item prop="birth">
            <el-date-picker type="date" placeholder="选择日期" v-model="InfoList.birth" style="width: 100%">
            </el-date-picker>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogInfoFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upUserInfoForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  //引入文件上传路径 和文件预览的路径
  import {
    showFileURL,
    uploadFileURL
  } from "@/utils/config";
  import moment from "moment";
  // 引入axios
  import {
    get,
    post
  } from '@/utils/request'
  export default {
    data() {
      return {
        imageUrl: '',
        uploadFileURL,
        id: '',
        // 数据源
        InfoList: {},
        //  对用户信息修改的模态框
        dialogInfoFormVisible: false,
        // 对用户头像修改的模态框
        ImgdialogVisible: false,
        // 对头像修改的表单
        upImageForm: {},

        //对用户信息修改的表单的验证
        rules: {
          realname: [{
            required: true,
            message: "请输入姓名",
            trigger: "blur"
          }],
          gender: [{
            required: true,
            message: "请选择性别",
            trigger: "change"
          }, ],
        },

      };
    },
    computed: {},
    methods: {
      // 点击打开修改头像模态框
      Upimg() {
        this.ImgdialogVisible = true;
        this.upImageForm.id = this.InfoList.id;
        this.upImageForm.userface = this.InfoList.userFace;
      },
      // 提交用户头像
      async upImage() {
        let res = await post("/baseUser/alterUserface", this.upImageForm);
        if (res.status == 200) {
          this.$message({
            message: res.message,
            type: "success",
          });
        }
        this.ImgdialogVisible = false;
        this.getInfoData(this.InfoList.id);
      },
      // 上传成功后回调的
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.upImageForm.userface = showFileURL + res.data.id;
      },
      // 在上传头像前做的操作
      beforeAvatarUpload(file) {
        const isLt3M = file.size / 1024 / 1024 < 3;
        if (!isLt3M) {
          this.$message.error("上传头像图片大小不能超过 3MB!");
        }
      },
      // 打开用户信息修改模态框
      upInfo() {
        this.dialogInfoFormVisible = true;
      },
      // 修改用户信息
      upUserInfoForm() {
        this.InfoList.birth = moment(this.InfoList.birth).valueOf();
        // console.log(this.InfoList, "--------");

        this.$refs["ruleForm"].validate(async (valid) => {
          if (valid) {
            let res = await post("/baseUser/saveOrUpdate", this.InfoList);
            if (res.status == 200) {
              this.$message({
                message: res.message,
                type: "success",
              });
            }

            this.dialogInfoFormVisible = false;
            this.getInfoData(this.InfoList.id);
          } else {}
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      // 根据id获取信息
      async getInfoData(id) {
        let res = await get('/baseUser/findUserDetailsById', {
          id
        });
        this.InfoList = res.data
        this.imageUrl = ''

      }

    },

    created() {
      // 接收获取到的参数
      this.id = this.$route.query.id
      // 调用获取全部数据的方法
      this.getInfoData(this.id);
    },
    mounted() {},
  };

</script>
<style scoped>
  .footer {
    float: right;
  }

  .userInfo {
    margin-top: 10px;
  }

  .el-form-item {
    margin-bottom: 8px;
    margin-left: 10px;
  }

  .avatar img {
    margin: 0 auto;
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }

  .right span {
    color: #2cb5ac;
    font-size: 11px;
    right: 0;
    position: absolute;
    bottom: 0;
  }

  .right {
    position: relative;
    width: 220px;
    height: 82px;
  }

  .left {
    width: 220px;
    height: 82px;
  }

  .header {
    align-items: center;
    justify-content: space-around;
    display: flex;
    padding: 16px 200px;
    width: 100%;
    height: 150px;
    border-bottom: 2px solid #f4f4f4;
  }

</style>
